package com.qf.controller;

import com.qf.common.R;
import com.qf.entity.LoginUser;
import com.qf.entity.PageVO;
import com.qf.entity.Search;
import com.qf.entity.User;
import com.qf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@Slf4j
@CrossOrigin
public class  UserController {
    @Autowired
    private UserService userService;
    //登录
    @PostMapping("/login")
    public R login(@RequestBody LoginUser loginUser){
        log.debug("用户登录：{}",loginUser);
        User user = userService.login(loginUser);
        if (user==null) {
            return R.fail("登录失败");
        }
        return R.ok(user);
    }


    @GetMapping("/list")
    public R list(Search search){
       List<User> list =userService.list(search);
       if (list==null ||list.size()==0){
           return R.fail("没有用户");
       }
        PageVO pageVO = new PageVO();
        pageVO.setPages(search.getPageNum());
        pageVO.setPageSize(search.getPageSize());
        pageVO.setPageNum(search.getTotal());
        pageVO.setTotal(search.getPages());
        pageVO.setList(list);
        return R.ok(pageVO);

    }
    @GetMapping("/del/batch")
    public R delete(@RequestParam List<Integer> ids){
        log.debug("批量删除：{}",ids);
        int count = userService.delete(ids);
        if (count>0){
            return R.fail("删除成功");
        }
        return R.ok(count);
    }

    @PostMapping("/del/batch")
    public R delete(@RequestParam User user){
        log.debug("批量删除：{}",user);
        int count = userService.add(user);
        if (count>0){
            return R.fail("删除成功");
        }
        return R.ok(count);
    }
}


//批量删除

